import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../Login.vue'
import Register from '../Register.vue'
import Home from '../components/Home.vue'
import Index from '../components/index/Index.vue'
import Personal from '../components/personal/Personal.vue'
import Account from '../components/personal/Account.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  {
    path: '/home', component: Home,redirect: '/index', children:[
      {path: '/index', component: Index},
      {path: '/personal', component: Personal,redirect: '/account',children:[
        {path:'/account',component:Account}
      ]},
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/index' || to.path === '/login' || to.path === '/register' || to.path === '/invest')
    return next()
  if (!window.sessionStorage.getItem('isLogin')) return next('/login')
  next()
})

export default router
